<?php

class Scaff {
    
    private $table;
    
    public function __construct($table) {
        $this->table = $table;
    }
    
    public function getTable() {
        return $this->table;
    }
    
    public function getAll($orderby = '', $direction = 'ASC') {
        $arr = array();
        $q = "SELECT * FROM $this->table ";
        if ($orderby) $q .= "ORDER BY $orderby ".$direction;
        if ($ok = mysql_query($q))
        {
            while($row = mysql_fetch_array($ok))
                array_push($arr, $row);
        }
        else die(mysql_error().' '.$q);
        return $arr;
    }
    
    public function getByPage($limit, $page = 1, $orderby = '', $direction = 'ASC', $field = '', $value = '') {
        $arr = array();
        $q = "SELECT * FROM $this->table ";
        if ($field) $q .= " WHERE `$field`='$value'";
        if ($orderby) $q .= " ORDER BY $orderby ".$direction;
        $q .= " LIMIT $limit";
        if ($ok = mysql_query($q))
        {
            while($row = mysql_fetch_array($ok))
                array_push($arr, $row);
        }
        else die(mysql_error().' '.$q);
        return $arr;
    }
    
    public function add($arr) {
        $q = "INSERT INTO $this->table (";
        foreach($arr as $k => $v)
        {
            if($k != 'submit') $q .= "`".$k."`, "; 
        }
        $q = trim($q, ', ').") VALUES (";
        foreach($arr as $k => $v)
        {
            if($k != 'submit')
            {
                if($v != 'NOW()') $q .= "'".$v."', "; 
                else $q .= $v.", "; 
            }
        }
        $q = trim($q, ', ').")";
        if (mysql_query($q))
        {
            setsession('success',"Elemenentul a fost adaugat");
            return mysql_insert_id();
        }
        else die(mysql_error().' '.$q);
    }
    
    public function del($field, $value) {
        $q = "DELETE FROM $this->table WHERE `$field`='$value'";
        if (mysql_query($q))
        {
            setsession('success',"Elementul a fost sters");
            return true;
        }
        else die(mysql_error().' '.$q);
    }
    
    public function get($field, $value) {
        $q = "SELECT * FROM $this->table WHERE `$field`='$value' ";
        if ($ok = mysql_query($q))
        {
            if($row = mysql_fetch_array($ok))
                return $row;
        }
        else die(mysql_error().' '.$q);
    }
    
    public function getCustomRow($q) {
        if ($ok = mysql_query($q))
        {
            if($row = mysql_fetch_array($ok))
                return $row;
        }
        else die(mysql_error().' '.$q);
    }
    
    public function getCustomRows($q) {
        $rows = array();
        if ($ok = mysql_query($q))
        {
            while($row = mysql_fetch_array($ok))
                array_push($rows, $row);
        }
        else die(mysql_error().' '.$q);
        return $rows;
    }
    
    public function update($arr, $field, $value) {
        $q = "UPDATE $this->table SET ";
        foreach($arr as $k => $v)
        {
            if($k != 'submit')
            {
                if($v != 'NOW()') $q .= "`".$k."`='".$v."', "; 
                else $q .= "`".$k."`=".$v.", "; 
            }
        }
        $q = trim($q, ', ')." WHERE `$field`='$value'";
        if (mysql_query($q))
        {
            setsession('success',"Elementul a fost actualizat");
            return 1;
        }
        else die(mysql_error().' '.$q);
    }
    
    
}

